package com.example.harmans;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TextView.BufferType;
import android.widget.Toast;

public class AboutActivity extends Activity {
    
	private String[] drawerListViewItems;
    private DrawerLayout drawerLayout;
    private ListView drawerListView;
    private ActionBarDrawerToggle actionBarDrawerToggle;
	String content;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_about);
		StrictMode.ThreadPolicy policy = new StrictMode.
	    ThreadPolicy.Builder().permitAll().build();
	    StrictMode.setThreadPolicy(policy); 
		
		ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("Information");
        actionBar.setSubtitle("About");

        // get list items from strings.xml
        drawerListViewItems = getResources().getStringArray(R.array.items);
        // get ListView defined in activity_main.xml
        drawerListView = (ListView) findViewById(R.id.left_drawer);

        // Set the adapter for the list view
        drawerListView.setAdapter(new ArrayAdapter<String>(this,R.layout.drawer_listview_item, drawerListViewItems));
 
        // App Icon 
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
 
        actionBarDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                drawerLayout,         /* DrawerLayout object */
                R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description */
                R.string.drawer_close  /* "close drawer" description */
        );
 
        // Set actionBarDrawerToggle as the DrawerListener
        drawerLayout.setDrawerListener(actionBarDrawerToggle);
 
        getActionBar().setDisplayHomeAsUpEnabled(true); 
 
        // just styling option add shadow the right edge of the drawer
        drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
		drawerListView.setOnItemClickListener(new DrawerItemClickListener());

        final TabHost tabs = (TabHost)findViewById(R.id.TabHost01);
        tabs.setup();
        TabHost.TabSpec spec1 = tabs.newTabSpec("tag1");
        spec1.setContent(R.id.tab1layout);
        tabs.getTabWidget().setDividerDrawable(null);
        spec1.setIndicator("About");
        tabs.addTab(spec1);

        TabHost.TabSpec spec2 = tabs.newTabSpec("tag2");
        spec2.setContent(R.id.tab2layout);
        spec2.setIndicator("FAQ");
        tabs.addTab(spec2);
        tabs.setCurrentTab(0);

        String getContent;
        HttpPost httppost = new HttpPost("http://www.mtcodelab.com/Development/harmans/about");
        getContent = "about";
        getWebContent(httppost,getContent);
        HttpPost httppost2 = new HttpPost("http://www.mtcodelab.com/Development/harmans/faq");
        getContent = "faq";
        getWebContent(httppost2,getContent);
	}

	@Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
         actionBarDrawerToggle.syncState();
    }

	private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
            String link = (String) ((TextView)view).getText();

            if(link.equals("Home")) {
        		Intent homeActivity = new Intent(AboutActivity.this,MainActivity.class);
        		startActivity(homeActivity);
            } else if(link.equals("Find A Court")) {
        		Intent courtActivity = new Intent(AboutActivity.this,CourtActivity.class);
		    	startActivity(courtActivity);
            } else if(link.equals("Court Fees")) {
        		Intent feesActivity = new Intent(AboutActivity.this,FeesActivity.class);
		    	startActivity(feesActivity);
            } else if(link.equals("Book A Collection")) {
        		Intent collectionActivity = new Intent(AboutActivity.this,CollectionActivity.class);
		    	startActivity(collectionActivity);
            } else if(link.equals("Interest Calculator")) {
        		Intent calcActivity = new Intent(AboutActivity.this,CalculatorActivity.class);
		    	startActivity(calcActivity);
            } else if(link.equals("FAQ")) {
        		Intent faqActivity = new Intent(AboutActivity.this,FaqActivity.class);
		    	startActivity(faqActivity);
            } else if(link.equals("Contact")) {
        		Intent contactActivity = new Intent(AboutActivity.this,ContactActivity.class);
		    	startActivity(contactActivity);
            } else if(link.equals("About")) {
    			Intent aboutActivity = new Intent(AboutActivity.this,AboutActivity.class);
		    	startActivity(aboutActivity);
            }
            drawerLayout.closeDrawer(drawerListView);
        }
    }

	public void getWebContent(HttpPost httppost, String getContent) {
		DefaultHttpClient   httpclient = new DefaultHttpClient(new BasicHttpParams());
        // Depends on your web service
        httppost.setHeader("Content-type", "application/json");

        InputStream inputStream = null;
        String result = null;
        try {
            HttpResponse response = httpclient.execute(httppost);           
            HttpEntity entity = response.getEntity();

            inputStream = entity.getContent();

            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();

            String line = null;
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
            result = sb.toString();
        } catch (Exception e) {
            // Oops
        }
        finally {
            try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
        }
        
        try {
			JSONObject jObject = new JSONObject(result);
			content = jObject.getString("content");
			
		} catch (JSONException e) {
			e.printStackTrace();
		}

        TextView about = (TextView)findViewById(R.id.about_text);
        TextView faq = (TextView)findViewById(R.id.faq_text);

        if(getContent=="about")
        	about.setText(Html.fromHtml(content), TextView.BufferType.SPANNABLE);
        else
        	faq.setText(Html.fromHtml(content), TextView.BufferType.SPANNABLE);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.about, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId() == android.R.id.home) {
			startActivity(new Intent(AboutActivity.this,MainActivity.class)); 
			return true;
		}
	    if(item.getItemId() == R.id.icon_calculator) {
	    	Intent calcIntent = new Intent(AboutActivity.this,CalculatorActivity.class);
	    	startActivity(calcIntent);
	    }
		if(item.getItemId() == R.id.menu_home) {	    	
			Intent mainIntent = new Intent(AboutActivity.this,MainActivity.class);
			startActivity(mainIntent);
	    }
	    if(item.getItemId() == R.id.menu_court) {
	    	Intent courtIntent = new Intent(AboutActivity.this,CourtActivity.class);
	    	startActivity(courtIntent);
	    }
	    if(item.getItemId() == R.id.menu_fees) {
	    	Intent feesIntent = new Intent(AboutActivity.this,FeesActivity.class);
	    	startActivity(feesIntent);
	    }
	    if(item.getItemId() == R.id.menu_collection) {
	    	Intent collectionIntent = new Intent(AboutActivity.this,CollectionActivity.class);
	    	startActivity(collectionIntent);
	    }
	    if(item.getItemId() == R.id.menu_faq) {
	    	Intent faqIntent = new Intent(AboutActivity.this,FaqActivity.class);
	    	startActivity(faqIntent);
	    }
	    if(item.getItemId() == R.id.menu_contact) {
	    	Intent contactIntent = new Intent(AboutActivity.this,ContactActivity.class);
	    	startActivity(contactIntent);
	    }
	    return true;
	}

}
